﻿using DAL;
using DAL.DBManagers;
using DAL.Models;
using System;
using System.Web.UI;
using System.Linq;

public partial class VesselEdit : System.Web.UI.Page
{
    Guid RecordID = Guid.Empty;
    Guid VesselID = Guid.Empty;
    VesselsManager manager = new VesselsManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = String.Empty;

        if (Guid.TryParse(Request.QueryString["id"], out RecordID))
        {
            if (!IsPostBack)
            {
                try
                {
                    var vessel = manager.Single(s => s.RecordID == RecordID);

                    txtName.Text = vessel.Name;
                    ddlVesselTypes.SelectedValue = vessel.VesselTypeID.ToString();
                    ddlFlags.SelectedValue = vessel.FlagID.ToString();
                    txtBeam.Text = vessel.Beam.ToString();
                    txtDepth.Text = vessel.Depth.ToString();
                    txtImo.Text = vessel.IMO.ToString();
                    txtLoa.Text = vessel.LOA.ToString();
                    txtGross.Text = vessel.Gross.ToString();
                    txtDeadweight.Text = vessel.Deadweight.ToString();
                    txtDraft.Text = vessel.Draft.ToString();
                    IsSbt.Checked = vessel.SBT;
                    txtBeginDate.DateValue = vessel.BeginDate;

                    //lblCreateDate.Visible = true;
                    //lblCreateDate.Text = String.Format("დამატების თარიღი: {0}", vessel.CreateDate.ToString("dd/MM/yyyy"));
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message;
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            try
            {
                var CurrentUser = Session["User"] as UserHelper;

                Guid NewVesselID = Guid.Empty;

                if (RecordID == Guid.Empty)
                {
                    if (Guid.TryParse(Request.QueryString["sid"], out VesselID))
                    {
                        if (manager.Find(f => f.IsActive && f.VesselID == VesselID && f.BeginDate == txtBeginDate.DateValue.Value).Any())
                        {
                            throw new Exception("Vessel already exists with this date");
                        }

                        manager.Add(new Vessel
                        {
                            RecordID = Guid.NewGuid(),
                            VesselID = VesselID,
                            FlagID = Guid.Parse(ddlFlags.SelectedValue),
                            VesselTypeID = Guid.Parse(ddlVesselTypes.SelectedValue),
                            Name = txtName.Text,
                            Beam = double.Parse(txtBeam.Text),
                            Depth = double.Parse(txtDepth.Text),
                            IMO = double.Parse(txtImo.Text),
                            LOA = double.Parse(txtLoa.Text),
                            Gross = double.Parse(txtGross.Text),
                            Deadweight = double.Parse(txtDeadweight.Text),
                            Draft = double.Parse(txtDraft.Text),
                            SBT = IsSbt.Checked,
                            BeginDate = txtBeginDate.DateValue.Value,
                            IsActive = true,
                            CreateDate = DateTime.Now,
                            CreatedBy = CurrentUser.UserID
                        });

                        NewVesselID = VesselID;
                    }
                    else
                    {
                        var vessel = new Vessel
                        {
                            RecordID = Guid.NewGuid(),
                            VesselID = Guid.NewGuid(),
                            FlagID = Guid.Parse(ddlFlags.SelectedValue),
                            VesselTypeID = Guid.Parse(ddlVesselTypes.SelectedValue),
                            Name = txtName.Text,
                            Beam = double.Parse(txtBeam.Text),
                            Depth = double.Parse(txtDepth.Text),
                            IMO = double.Parse(txtImo.Text),
                            LOA = double.Parse(txtLoa.Text),
                            Gross = double.Parse(txtGross.Text),
                            Deadweight = double.Parse(txtDeadweight.Text),
                            Draft = double.Parse(txtDraft.Text),
                            SBT = IsSbt.Checked,
                            BeginDate = txtBeginDate.DateValue.Value,
                            IsActive = true,
                            CreateDate = DateTime.Now,
                            CreatedBy = CurrentUser.UserID
                        };

                        manager.Add(vessel);

                        NewVesselID = vessel.VesselID;
                    }
                }
                else
                {
                    var vessel = manager.Single(s => s.RecordID == RecordID);

                    if (manager.Find(f => f.IsActive && f.VesselID == vessel.VesselID && f.RecordID != RecordID && f.BeginDate == txtBeginDate.DateValue.Value).Any())
                    {
                        throw new Exception("Vessel already exists with this date");
                    }

                    vessel.Name = txtName.Text;
                    vessel.FlagID = Guid.Parse(ddlFlags.SelectedValue);
                    vessel.VesselTypeID = Guid.Parse(ddlVesselTypes.SelectedValue);
                    vessel.Beam = double.Parse(txtBeam.Text);
                    vessel.Depth = double.Parse(txtDepth.Text);
                    vessel.IMO = double.Parse(txtImo.Text);
                    vessel.LOA = double.Parse(txtLoa.Text);
                    vessel.Gross = double.Parse(txtGross.Text);
                    vessel.Deadweight = double.Parse(txtDeadweight.Text);
                    vessel.Draft = double.Parse(txtDraft.Text);
                    vessel.SBT = IsSbt.Checked;
                    vessel.BeginDate = txtBeginDate.DateValue.Value;
                    vessel.LastModifyDate = DateTime.Now;
                    vessel.LastModifier = CurrentUser.UserID;

                    NewVesselID = vessel.VesselID;
                }

                manager.SaveChanges();
                manager.RepiarDates(NewVesselID);

                if (VesselID == Guid.Empty && RecordID == Guid.Empty)
                {
                    Response.Redirect("vesselview.aspx?m=liVessel");
                }
                else
                {
                    Response.Redirect(String.Format("~/vesselhistory.aspx?id={0}&m={1}", NewVesselID, "liVessel"));
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
    }
}